package util;


import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/9/15.
 */
public class ValidatorUtil {
    /**
     * 校验用户名
     *
     * @param username
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isUserName(String username) {
        return Pattern.matches(PatternList.REGEX_USERNAME, username);
    }

    /**
     * 校验密码
     *
     * @param password
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isPassword(String password) {
        return Pattern.matches(PatternList.REGEX_PASSWORD, password);
    }

    /**
     * 校验手机号
     *
     * @param mobile
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isMobile(String mobile, boolean power) {

        if (StringUtils.isEmpty(mobile)) {
            return !power;
        } else {
            return Pattern.matches(PatternList.REGEX_MOBILE, mobile);
        }


    }

    /**
     * 校验手机号
     *
     * @param mobile
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isMobile(String mobile) {
        return isMobile(mobile, true);
    }

    /**
     * 校验邮箱
     *
     * @param email
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isEmail(String email, boolean power) {

        if (StringUtils.isEmpty(email)) {
            return !power;
        } else {
            return Pattern.matches(PatternList.REGEX_EMAIL, email);
        }

    }

    /**
     * 校验邮箱
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        return isEmail(email, true);
    }

    /**
     * 校验汉字
     *
     * @param chinese
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isChinese(String chinese, boolean power) {

        if (StringUtils.isEmpty(chinese)) {
            return !power;
        } else {
            return Pattern.matches(PatternList.REGEX_CHINESE, chinese);
        }


    }

    /**
     * 校验身份证
     *
     * @param idCard
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isIdCard(String idCard, boolean power) {

        if (StringUtils.isEmpty(idCard)) {
            return !power;
        } else {
            return IdCardUtil.validateIdCard(idCard);
        }

    }

    /**
     * 校验URL
     *
     * @param url
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isUrl(String url, boolean power) {
        if (StringUtils.isEmpty(url)) {
            return !power;
        } else {
            return Pattern.matches(PatternList.REGEX_URL, url);
        }

    }

    /**
     * 校验IP地址
     *
     * @param ip
     * @return
     */
    public static boolean isIP(String ip, boolean power) {
        if (StringUtils.isEmpty(ip)) {
            return !power;
        } else {
            return Pattern.matches(PatternList.REGEX_IP_ADDR, ip);
        }
    }

    /**
     * 校验IP地址
     *
     * @param ip
     * @return
     */

    /**
     * 验证 key 是否存在 valueBase 中
     *
     * @param key
     * @param valueBase
     * @return
     */
    public static boolean isExistence(String key, String... valueBase) {

        if (valueBase != null) {
            for (String s : valueBase) {
                if (StringUtils.equals(key, s)) {
                    return true;
                }
            }
        }
        return false;
    }

//    public static void main(String[] args) {
////        System.out.println(isExistence("1", null));
//    }


}
